package com.jindun.paper.ui.menu;

import org.eclipse.jface.action.ContributionItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;

public class BusinessMenuSwitcher extends ContributionItem {

	@Override
	public void fill(Menu menu, int index) {

		IPerspectiveDescriptor[] perspectives = PlatformUI.getWorkbench()
				.getPerspectiveRegistry().getPerspectives();

		int i = 0;
		for (final IPerspectiveDescriptor perpective : perspectives) {

			MenuItem item = new MenuItem(menu, SWT.RADIO, i++);
			item.setText(perpective.getLabel());

			item.addSelectionListener(new SelectionListener() {
				@Override
				public void widgetSelected(SelectionEvent e) {

					try {
						PlatformUI.getWorkbench().showPerspective(
								perpective.getId(),
								PlatformUI.getWorkbench()
										.getActiveWorkbenchWindow());
					} catch (WorkbenchException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

				}

				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
					// TODO Auto-generated method stub

				}
			});

		}
	}

}
